package com.yingjia.yingjiauniversity.utils;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.my.common.R;

/**
 * Created by ning.li.o on 2017/11/30.
 */

public class CheckImageViewAnimation extends android.support.v7.widget.AppCompatImageView {

    private boolean isShowAnimation;
    private int padding;
    private int paddingLeft;
    private int paddingRight;
    private int paddingTop;
    private int paddingBottom;

    private OnClickImageViewListener onClickImageViewListener;

    public interface OnClickImageViewListener {
        void onClickImageView();
    }

    public void setOnClickImageViewListener(OnClickImageViewListener onClickImageViewListener) {
        this.onClickImageViewListener = onClickImageViewListener;
    }

    public CheckImageViewAnimation(Context context) {
        this(context, null);
    }

    public CheckImageViewAnimation(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CheckImageViewAnimation(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.checkImageView, defStyleAttr, 0);

        isShowAnimation = typedArray.getBoolean(R.styleable.checkImageView_img_show_animation, true);
        padding = typedArray.getDimensionPixelSize(R.styleable.checkImageView_img_padding, 0);
        paddingLeft = typedArray.getDimensionPixelSize(R.styleable.checkImageView_img_padding_left, 0);
        paddingRight = typedArray.getDimensionPixelSize(R.styleable.checkImageView_img_padding_right, 0);
        paddingTop = typedArray.getDimensionPixelSize(R.styleable.checkImageView_img_padding_top, 0);
        paddingBottom = typedArray.getDimensionPixelSize(R.styleable.checkImageView_img_padding_bottom, 0);
        typedArray.recycle();
//        if ((paddingLeft == 0 || paddingRight == 0 || paddingTop == 0 || paddingBottom == 0) && isShowAnimation) {
//            paddingLeft = DeviceUtil.dip2px(10);
//            paddingRight = DeviceUtil.dip2px(10);
//            paddingTop = DeviceUtil.dip2px(10);
//            paddingBottom = DeviceUtil.dip2px(10);
//        }
//
//        setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
//
//        if (padding != 0) {
//            setPadding(padding, padding, padding, padding);
//        }

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowAnimation) {
                    playSelectedAnimation(v);
                }

                if (onClickImageViewListener != null) {
                    onClickImageViewListener.onClickImageView();
                }
            }
        });
    }

    private void playSelectedAnimation(View view) {
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.3f, 1f);//, 0.8f
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.3f, 1f);//, 0.8f

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
        animatorSet.setDuration(500);
//        animatorSet.setInterpolator(new BounceInterpolator());
        animatorSet.start();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        int width = MeasureSpec.getSize(widthMeasureSpec);
//        int height = MeasureSpec.getSize(heightMeasureSpec);
//
//        com.nio.vomcore.log.Logger.d("width", width + "");
//        com.nio.vomcore.log.Logger.d("height", height + "");
//
//        int paddingl = getPaddingLeft();
//        int paddingr = getPaddingRight();
//        int paddingt = getPaddingTop();
//        int paddingb = getPaddingBottom();
//
//
//        width = width + paddingl + paddingr;
//        height = height + paddingb + paddingt;
//
//
//        setMeasuredDimension(width, height);

//        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
//        layoutParams.width = width;
//        layoutParams.height = height;
//        setLayoutParams(layoutParams);
    }
}
